/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lt.cj.core.filters;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.lt.cj.config.entities.ConfigModel;
import org.lt.cj.config.entities.TMallConfigModel;
import org.lt.cj.core.Seed;
import org.lt.cj.core.builder.TMallBuilder;
import org.lt.cj.core.entity.Entity;
import org.lt.cj.core.entity.TMallEntity;
import org.lt.cj.core.utils.HttpAccesser;

/*
 * @author luohong
 */
public class TMallEntityFilter implements EntityFilter {

    private TMallConfigModel configModel;
    private TMallBuilder builder;

    public TMallEntityFilter(ConfigModel configModel) {
        this.configModel = (TMallConfigModel) configModel;
        builder = new TMallBuilder();
    }

    public Entity filtEntity(Seed seedURL) {

        String url = seedURL.getUrl();
        HttpAccesser accesser = HttpAccesser.getInstance();//获取访问http地址的工具类
        String htmlText = accesser.accessWebPage(url);
        HierPathFilter filter = new HierPathFilter(configModel, htmlText);
        TMallConfigModel tmallConfigModel = (TMallConfigModel) configModel;
        //获取过滤匹配路径信息
        Map<String, List<String>> map = tmallConfigModel.getEntityReg();
        List<String> productNameList = map.get("productName");
        List<String> productPriceList = map.get("productPrice");
        List<String> countSoldList = map.get("countSold");
        List<String> shopInfoList = map.get("shopInfo");
        List<String> productDetailList = map.get("productDetail");
        List<String> photosPathList = map.get("photosPath");
        List<String> thumbPhosPathList = map.get("thumbPhosPath");
        List<String> despPhosPathList = map.get("despPhos");
        List<String> shopAddrPathList = map.get("shopAddr");
        List<String> commnentList = map.get("commnents");

        //构建URL
        builder.buildProductURL(url);

        //构建分类信息
        builder.buildCategory(seedURL.getSortName());

        //过滤出评论的内容并构造出数据实体的
        String commentContent = filter.filtContent(commnentList, 0, "org.lt.cj.core.filters.DivTagCustomFilter");
        List<String> commentList = new ArrayList();
        commentList.add(commentContent);
        builder.buildComments(commentList);

        //过滤出网店的地址并构造网店的地址部件
        builder.buildShopAddr(filter.filtContent(shopAddrPathList, 0, "org.lt.cj.core.filters.LinkTagCustomFilter"));

        //过滤出产品详情的图片并构造数据实体的产品详情的图片路径部件
        builder.buildProductDespPhosPaths(filter.filtScript(despPhosPathList, FilterConstans.contentPointed, 2));
        //过滤出产品详情的文字并构造数据实体的产品详情的文字部件
//        builder.buildProductDespMemoPaths(filter.filtContents(despMemosPathList, FilterConstans.contentsEvery));

        //过滤出图标信息并构造数据实体的图标路径部件
        builder.buildProductThumbsPaths(filter.filtPhoto(thumbPhosPathList, FilterConstans.contentsEvery));
        //过滤出图片信息并构造数据实体的图片部件
        builder.buildphotoPaths(filter.filtPhoto(photosPathList, FilterConstans.contentsEvery));
        //过滤出商品属性信息并构造数据实体的商品属性部件
        builder.buildProductDetails(filter.filtContents(productDetailList, FilterConstans.contentsEven));
        builder.buildCompanyName(filter.filtContent(shopInfoList, 0));      //过滤出产品的公司名并构造数据实体的产品的公司名部件
        builder.buildSuoZaiDi(filter.filtContent(shopInfoList, 2));         //过滤出产品的公司所在地并构造数据实体的产品的公司所在地部件
        builder.buildShangJia(filter.filtContent(shopInfoList, 4));         //过滤出产品的公司商家并构造数据实体的产品的公司商家部件


        builder.buildCountSold(filter.filtContent(countSoldList, 0));           //过滤出已售产品数量并构造数据实体的产品的已售产品数量部件
        builder.buildProductprice(filter.filtContent(productPriceList, 0));     //过滤出产品价格并构造数据实体的产品价格部件
        builder.buildProductName(filter.filtContent(productNameList, 0));       //过滤出产品名称并构造数据实体的产品名部件

        /* 设置语言 */
        builder.buildOrigLanguage(configModel.getOrigLanguage());
        builder.buildTranLanguage(configModel.getTranLanguage());

        TMallEntity tmallEntity = (TMallEntity) builder.getProduct();
        System.out.println("url:" + tmallEntity.getProductUrl().trim());
        System.out.println("pName:" + tmallEntity.getProductName().trim());
        System.out.println("pcategory" + tmallEntity.getCategory().trim());
        System.out.println("pprice" + tmallEntity.getPrice().trim());
        System.out.println(tmallEntity.getCountSold().trim());
        System.out.println(tmallEntity.getCompanyName().trim());
        return tmallEntity;
    }

    /* 过滤出产品的名称 */
    public String filtProductName() {

        return "";
    }

    public ConfigModel getConfigModel() {
        return configModel;
    }

    public void setConfigModel(ConfigModel configModel) {
        this.configModel = (TMallConfigModel) configModel;
    }

    public TMallBuilder getBuilder() {
        return builder;
    }

    public void setBuilder(TMallBuilder builder) {
        this.builder = builder;
    }
}
